package com.wzg.appframe.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.wzg.appframe.R;
import com.wzg.appframe.utils.TLog;
import com.wzg.appframe.web.BaseWebActivity;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TestActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.button)
    Button scanBtn;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {


        scanBtn.setOnClickListener(this);
    }


    /**
     * 扫描跳转Activity RequestCode
     */
    public static final int REQUEST_CODE = 001;

    void scanCode() {
        Intent intent = new Intent(getApplication(), CaptureActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
//                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                    openNewPage(result);
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }

    }

    private void openNewPage(String url) {
//        JSONObject mJson = new JSONObject(str);
        Intent intent = new Intent(this, BaseWebActivity.class);
        intent.putExtra(BaseWebActivity.PAGE, url);
        startActivity(intent);

    }

    @Override
    public void onClick(View v) {
        if (v == scanBtn) {
            scanCode();
        }
    }
}
